package com.webdesign.action;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.webdesign.entity.TbAdmin;
import com.webdesign.entity.TbTeacher;
import com.webdesign.entity.TbUser;
import com.webdesign.service.ITbAdminService;
import com.webdesign.service.ITbTeacherService;
import com.webdesign.service.ITbUserService;
import com.webdesign.util.MD5;


@Controller("LoginAction")
@Scope("prototype")
public class LoginAction {
	
	private static final String REDIRECT="redirect";
	private ITbAdminService iTbAdminService;
	private ITbUserService iTbUserService;
	private ITbTeacherService iTbTeacherService;
	private String username;
	private String password;
	private String flag;
	
	
	/*
	 * 首页点击注册链接
	 */
	public String homepageRegister(){
		return "success";
	}
	
	
	/*
	 * 首页点击登录链接
	 */
	public String homepageLogin(){
		return "success";
	}
	
	/*
	 * 登录功能，先判断是不是教师登录，再判断是不是学生登录，要是登录成功则分别跳转到各自的首页，否则留在登录页面
	 */
	public String userLogin(){
		MD5 md = new MD5();
		String passwordMD5 = md.MD5(password);
		TbAdmin tbAdmin = iTbAdminService.login(username, passwordMD5);
		if(tbAdmin != null){//判断管理员数据库中是否有该username，如果有
			ActionContext.getContext().getSession().put("username",tbAdmin.getUsername());//放到前台session中
			//管理员跳转
			flag = "admin";
		}else{
			TbTeacher tbTeacher = iTbTeacherService.login(username, passwordMD5);
			if(tbTeacher != null){
				ActionContext.getContext().getSession().put("username", tbTeacher.getUsername());
				ActionContext.getContext().getSession().put("userid", tbTeacher.getUserid());
				flag = "teacher";
			}else{
				TbUser tbUser = iTbUserService.userLogin(username, passwordMD5);
				if(tbUser != null){
					ActionContext.getContext().getSession().put("username", tbUser.getUsername());
					ActionContext.getContext().getSession().put("userid", tbUser.getUserid());
					flag = "student";
				}else{
					flag = "wrong";
				}
			}
		}
		return "success";
	}
	
	/*
	 * 退出登录
	 */
	public String logout(){
		ActionContext.getContext().getSession().clear();
		return "success";
	}

	public ITbAdminService getiTbAdminService() {
		return iTbAdminService;
	}

	@Resource(name="TbAdminService")
	public void setiTbAdminService(ITbAdminService iTbAdminService) {
		this.iTbAdminService = iTbAdminService;
	}


	public ITbUserService getiTbUserService() {
		return iTbUserService;
	}

	@Resource(name="TbUserService")
	public void setiTbUserService(ITbUserService iTbUserService) {
		this.iTbUserService = iTbUserService;
	}


	public ITbTeacherService getiTbTeacherService() {
		return iTbTeacherService;
	}

	@Resource(name="TbTeacherService")
	public void setiTbTeacherService(ITbTeacherService iTbTeacherService) {
		this.iTbTeacherService = iTbTeacherService;
	}


	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public String getFlag() {
		return flag;
	}


	public void setFlag(String flag) {
		this.flag = flag;
	}
	
}
